package io.shallwe._admin.basedata.material;

import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.kit.Ret;
import io.shallwe.common.controller.BaseController;
import io.shallwe.common.model.Account;
import io.shallwe.common.model.MaterialNoName;

import java.util.Date;

/**
 * 物料名称对照表Controller
 * @author : 王小伟
 * @date : 2019-07-28 12:04:04
 */
public class MaterialNoNameController extends BaseController {
    @Inject
    MaterialNoNameService service;

    public void index(){
        render("index.html");
    }

    public void list(){
        Integer offset = getParaToInt("offset", 1);
        Integer pageSize = getParaToInt("pageSize", 10);
        String materialCode=get("materialCode");
        String remark=get("remark");
        renderJson(service.paginate(offset, pageSize, materialCode, remark));
    }

    public void add(){
        render("add_edit.html");
    }

    @Before(MaterialNoNameValidator.class)
    public void save(){
        MaterialNoName codeName = getBean(MaterialNoName.class, "codeName",true);
        codeName.setCreateAt(new Date());
        Account loginAccount = getLoginAccount();
        codeName.setCreateBy(loginAccount.getId());
        Ret ret = service.save(codeName);
        renderJson(ret);
    }

    public void edit(){
        Integer id = getInt("id");
        MaterialNoName codeName = service.findById(id);
        set("codeName",codeName);
        render("add_edit.html");
    }

    @Before(MaterialNoNameValidator.class)
    public void update(){
        MaterialNoName codeName = getBean(MaterialNoName.class, "codeName",true);
        Account loginAccount = getLoginAccount();
        codeName.setUpdateBy(loginAccount.getId());
        codeName.setUpdateAt(new Date());
        Ret ret = service.update(codeName);
        renderJson(ret);
    }

    public void delete(){
        String id = get("id");
        Ret ret = service.delete(id);
        renderJson(ret);
    }



}
